package com.youlai.system.controller.device;

import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.youlai.system.common.result.PageResult;
import com.youlai.system.pojo.entity.device.DeviceDataEntity;
import com.youlai.system.pojo.entity.device.DeviceLogEntity;
import com.youlai.system.pojo.query.DeviceDataQuery;
import com.youlai.system.pojo.vo.device.DeviceDataPageVO;
import com.youlai.system.service.impl.device.DeviceDataService;
import com.youlai.system.service.impl.device.DeviceService;
import com.youlai.system.utils.ExcelUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;
import java.util.stream.Collectors;

@Tag(name = "11.设备数据")
@RestController
@RequestMapping("/api/v1/device/data")
@RequiredArgsConstructor
public class DeviceDataController {


    private final DeviceDataService deviceDataService;
    private final DeviceService deviceService;

    @Operation(summary = "设备数据分页", security = {@SecurityRequirement(name = "Authorization")})
    @GetMapping("/page")
    public PageResult<DeviceDataPageVO> page(
            DeviceDataQuery queryParams
    ) {
        LambdaQueryWrapper<DeviceDataEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if(queryParams.getDeviceId() != null) {
            lambdaQueryWrapper.eq(DeviceDataEntity::getDeviceId, queryParams.getDeviceId());
        }
        if(StringUtils.isNotEmpty(queryParams.getDataType()) ) {
            lambdaQueryWrapper.eq(DeviceDataEntity::getDataType, queryParams.getDataType());
        }

        if( null != queryParams.getStartTime()) {
            lambdaQueryWrapper.gt(DeviceDataEntity::getCreateTime, new Date(queryParams.getStartTime()));
        }
        if(null != queryParams.getEndTime()) {
            lambdaQueryWrapper.lt(DeviceDataEntity::getCreateTime, new Date(queryParams.getEndTime()));
        }


        lambdaQueryWrapper.orderByDesc(DeviceDataEntity::getId);
        Page<DeviceDataEntity> page = new Page(queryParams.getPageNum(), queryParams.getPageSize());
        page = deviceDataService.page(page, lambdaQueryWrapper);


        IPage<DeviceDataPageVO> dataPageVOIPage = new Page<>();
        dataPageVOIPage.setPages(page.getPages());
        dataPageVOIPage.setTotal(page.getTotal());

        dataPageVOIPage.setRecords(page.getRecords()

                .stream().map(
                        deviceData -> {
                            DeviceDataPageVO deviceDataPageVO = new DeviceDataPageVO();
                            deviceDataPageVO.setData(deviceData.getData());
                            deviceDataPageVO.setDeviceName(deviceService.getById(deviceData.getDeviceId()).getName());
                            if("YW".equalsIgnoreCase(deviceData.getDataType())){
                                deviceDataPageVO.setDataType("液位");
                            }
                            if("PM".equalsIgnoreCase(deviceData.getDataType())){
                                deviceDataPageVO.setDataType("pm");
                            }
                            if("TEMP".equalsIgnoreCase(deviceData.getDataType())){
                                deviceDataPageVO.setDataType("温度");
                            }
                            if("RATE".equalsIgnoreCase(deviceData.getDataType())){
                                deviceDataPageVO.setDataType("频率");
                            }
                            deviceDataPageVO.setTime(new DateTime(deviceData.getCreateTime().getTime())
                                    .toString("yyyy-MM-dd HH:mm:ss"));
                            return deviceDataPageVO;
                        }
                ).collect(Collectors.toList()));

        return PageResult.success(dataPageVOIPage);
    }

    @Operation(summary = "导出", security = {@SecurityRequirement(name = "Authorization")})
    @GetMapping("/export")
    public void export(
            DeviceDataQuery queryParams,
            HttpServletResponse response
    )  throws Exception{
        LambdaQueryWrapper<DeviceDataEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if(queryParams.getDeviceId() != null) {
            lambdaQueryWrapper.eq(DeviceDataEntity::getDeviceId, queryParams.getDeviceId());
        }
        if(StringUtils.isNotEmpty(queryParams.getDataType()) ) {
            lambdaQueryWrapper.eq(DeviceDataEntity::getDataType, queryParams.getDataType());
        }

        if( null != queryParams.getStartTime()) {
            lambdaQueryWrapper.gt(DeviceDataEntity::getCreateTime, new Date(queryParams.getStartTime()));
        }
        if(null != queryParams.getEndTime()) {
            lambdaQueryWrapper.lt(DeviceDataEntity::getCreateTime, new Date(queryParams.getEndTime()));
        }


        lambdaQueryWrapper.orderByDesc(DeviceDataEntity::getId);

        List<DeviceDataEntity> deviceDataEntities  = deviceDataService.list(lambdaQueryWrapper);


       List<DeviceDataPageVO> pageVOS = deviceDataEntities

                .stream().map(
                        deviceData -> {
                            DeviceDataPageVO deviceDataPageVO = new DeviceDataPageVO();
                            deviceDataPageVO.setData(deviceData.getData());
                            deviceDataPageVO.setDeviceName(deviceService.getById(deviceData.getDeviceId()).getName());
                            if("YW".equalsIgnoreCase(deviceData.getDataType())){
                                deviceDataPageVO.setDataType("液位");
                            }
                            if("PM".equalsIgnoreCase(deviceData.getDataType())){
                                deviceDataPageVO.setDataType("pm");
                            }
                            if("RATE".equalsIgnoreCase(deviceData.getDataType())){
                                deviceDataPageVO.setDataType("频率");
                            }
                            if("TEMP".equalsIgnoreCase(deviceData.getDataType())){
                                deviceDataPageVO.setDataType("温度");
                            }
                            deviceDataPageVO.setTime(new DateTime(deviceData.getCreateTime().getTime())
                                    .toString("yyyy-MM-dd HH:mm:ss"));
                            return deviceDataPageVO;
                        }
                ).collect(Collectors.toList());
        ExcelUtils.write(response, "导出实时数据.xls", "数据", DeviceDataPageVO.class, pageVOS);

    }

}
